from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QWidget
from PyQt5.QtWidgets import (QApplication)
from PyQt5.QtCore import Qt, QPoint, pyqtSignal
from .album_blur_background import AlbumBlurBackground
import os
from modules import *
import resources_rc

class AlbumCard(QWidget):

    def __init__(self,albumInfo):
        super().__init__()

        # self.setWindowFlags(Qt.FramelessWindowHint)      #隐藏窗口边框
        # 去掉标题栏
        # self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowMinMaxButtonsHint)
        # # MainWindow.setWindowFlags(Qt.CustomizeWindowHint)
        self.cover = Config.coverPath + albumInfo[2] + '-' + albumInfo[1] + '.jpg'
        if os.path.exists(self.cover):
            pass
        else:
            self.cover = ''
        self.setupUi()
        self.initButton()
        self.bindData(albumInfo)
        # self.initSignal()
        # self.initHandle()
        # self.initGrip()
        # self.initSlider()
        # self.show()

    def setupUi(self):
        self.setObjectName("AlbumCard")
        self.resize(170, 230)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        self.setMinimumSize(QtCore.QSize(170, 230))
        self.setMaximumSize(QtCore.QSize(170, 230))
        self.setStyleSheet("#AlbumCoverBox .QPushButton{\n"
                                "    background-color:rgba(160,160,160,0.8);\n"
                                "    border-radius:25px;\n"
                                "}\n"
                                "\n"
                                "#AlbumCoverBox .QPushButton:hover{\n"
                                "    background-color:rgba(120,120,120,0.8);\n"
                                "}\n"
                                "\n"
                                "#AlbumDate{\n"
                                "    color:rgb(120,120,120);\n"
                                "}")
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.AlbumCoverBox = QtWidgets.QFrame(self)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.AlbumCoverBox.sizePolicy().hasHeightForWidth())
        self.AlbumCoverBox.setSizePolicy(sizePolicy)
        self.AlbumCoverBox.setMinimumSize(QtCore.QSize(170, 170))
        self.AlbumCoverBox.setMaximumSize(QtCore.QSize(170, 170))
        self.AlbumCoverBox.setToolTipDuration(-1)
        self.AlbumCoverBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.AlbumCoverBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.AlbumCoverBox.setLineWidth(1)
        self.AlbumCoverBox.setObjectName("AlbumCoverBox")
        self.AlbumCover = QtWidgets.QLabel(self.AlbumCoverBox)
        self.AlbumCover.setEnabled(True)
        self.AlbumCover.setGeometry(QtCore.QRect(0, 0, 170, 170))
        self.AlbumCover.setMinimumSize(QtCore.QSize(170, 170))
        self.AlbumCover.setMaximumSize(QtCore.QSize(170, 170))
        self.AlbumCover.setText("")
        self.AlbumCover.setScaledContents(True)
        self.AlbumCover.setObjectName("AlbumCover")
        self.PlayButton = QtWidgets.QPushButton(self.AlbumCoverBox)
        self.PlayButton.setGeometry(QtCore.QRect(30, 60, 50, 50))
        self.PlayButton.setMinimumSize(QtCore.QSize(50, 50))
        self.PlayButton.setMaximumSize(QtCore.QSize(50, 50))
        self.PlayButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/icons/images/icon/play_3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.PlayButton.setIcon(icon)
        self.PlayButton.setIconSize(QtCore.QSize(30, 30))
        self.PlayButton.setObjectName("PlayButton")
        self.AddToButton = QtWidgets.QPushButton(self.AlbumCoverBox)
        self.AddToButton.setGeometry(QtCore.QRect(90, 60, 50, 50))
        self.AddToButton.setMinimumSize(QtCore.QSize(50, 50))
        self.AddToButton.setMaximumSize(QtCore.QSize(50, 50))
        self.AddToButton.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/icons/images/icon/add.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.AddToButton.setIcon(icon1)
        self.AddToButton.setIconSize(QtCore.QSize(30, 30))
        self.AddToButton.setObjectName("AddToButton")
        self.verticalLayout.addWidget(self.AlbumCoverBox)
        self.AlbumInfoBox = QtWidgets.QFrame(self)
        self.AlbumInfoBox.setMaximumSize(QtCore.QSize(16777215, 58))
        self.AlbumInfoBox.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.AlbumInfoBox.setFrameShadow(QtWidgets.QFrame.Raised)
        self.AlbumInfoBox.setObjectName("AlbumInfoBox")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.AlbumInfoBox)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setSpacing(0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.AlbumTitle = QtWidgets.QLabel(self.AlbumInfoBox)
        self.AlbumTitle.setMaximumSize(QtCore.QSize(16777215, 38))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(11)
        font.setBold(True)
        self.AlbumTitle.setFont(font)
        self.AlbumTitle.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
        self.AlbumTitle.setWordWrap(True)
        self.AlbumTitle.setObjectName("AlbumTitle")
        self.verticalLayout_2.addWidget(self.AlbumTitle)
        self.AlbumDate = QtWidgets.QLabel(self.AlbumInfoBox)
        self.AlbumDate.setMaximumSize(QtCore.QSize(16777215, 20))
        self.AlbumDate.setTextInteractionFlags(QtCore.Qt.LinksAccessibleByMouse)
        self.AlbumDate.setObjectName("AlbumDate")
        self.verticalLayout_2.addWidget(self.AlbumDate)
        self.verticalLayout.addWidget(self.AlbumInfoBox)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("AlbumCard", "Form"))

    def initButton(self):
        self.PlayButton.hide()
        self.AddToButton.hide()

    # def initSignal(self):
    #     self.AlbumCover.clicked.connect(CommonSignal.showDetailSignal)

    def enterEvent(self, e):
        # print('---------enterEvent---------')
        # albumCardPos = self.mapToGlobal(QPoint(0, 0))

        self.PlayButton.show()
        self.AddToButton.show()
        if hasattr(self, 'albumBlurBackground'):
            self.albumBlurBackground.show()
            return
        albumCardPos = self.AlbumCover.mapToGlobal(QPoint(0, 0))
        pos = self.mapFromGlobal(albumCardPos)
        self.albumBlurBackground = AlbumBlurBackground(self)
        if self.cover == '':
            self.albumBlurBackground.setBlurAlbum(':/images/images/images/2.jpg')
        else:
            self.albumBlurBackground.setBlurAlbum(self.cover)

        self.albumBlurBackground.move(pos.x() - 28, pos.y() - 16)
        # self.label.raise_()
        self.albumBlurBackground.lower()
        self.albumBlurBackground.show()

    def mouseReleaseEvent(self,e):
        # print (list(e))
        CommonSignal.showDetailSignal.emit(self.AlbumId)
    def leaveEvent(self, e):
        # print('---------leaveEvent---------')
        self.albumBlurBackground.hide()
        self.PlayButton.hide()
        self.AddToButton.hide()

    def bindData(self,albumInfo):
        self.AlbumCover.setPixmap(QtGui.QPixmap(self.cover))
        self.AlbumTitle.setText(albumInfo[1])
        self.AlbumDate.setText(albumInfo[3])
        self.AlbumId = albumInfo[0]
        self.PlayButton.clicked.connect(lambda:CommonSignal.playAlbumSignal.emit(albumInfo[1],albumInfo[2]))
# import sys
#
# app = QApplication(sys.argv)
# ui = Ui_MainWindow()
#
# sys.exit(app.exec())